package com.small.site.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    private static RedisUtil redisUtil;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @PostConstruct
    public void init(){
        redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
    }

    public static void set(String key,String value){
        redisUtil.redisTemplate.opsForValue().set(key,value);
    }

    public static void set(String key,String value,Long expired){
        redisUtil.redisTemplate.opsForValue().set(key,value,expired, TimeUnit.SECONDS);
    }

    public static void get(String key){
        redisUtil.redisTemplate.opsForValue().get(key);
    }

    public static Boolean hasKey(String key){
        return redisUtil.redisTemplate.hasKey(key);
    }
}
